Rust – matoken's blog https://matoken.org/blog Is there no plan B? Mon, 22 Sep 2025 22:24:41 +0000 ja hourly 1 https://wordpress.org/?v=6.8.3 https://matoken.org/blog/wp-content/uploads/2025/03/cropped-1865f695c4eecc844385acef2f078255036adccd42c254580ea3844543ab56d9-32x32.jpeg Rust – matoken's blog https://matoken.org/blog 32 32 asciinema 3.0とRaspberry Pi armhf環境でのビルド https://matoken.org/blog/2025/09/23/building-in-asciinema-3-0-and-raspberry-pi-armhf-environments/ https://matoken.org/blog/2025/09/23/building-in-asciinema-3-0-and-raspberry-pi-armhf-environments/#respond Mon, 22 Sep 2025 22:08:00 +0000 https://matoken.org/blog/?p=4703

asciinema logo red

ターミナルの録画再生共有などができるソフトウェアのasciinemaの3.0がリリースされました.
今回のリリースはrustで書き直され,ファイル形式も新しくなったようです.(asciicast v3 file format)また,ターミナルのウェブによるライブストリーミング機能も付きました.

自分のDebian環境ではパッケージ度導入しているのですが,出たばかりなのでunstableでも2.4.0です.

$ rmadison asciinema
asciinema  | 2.0.2-2       | oldoldstable | source, all
asciinema  | 2.2.0-1       | oldstable    | source, all
asciinema  | 2.4.0-1       | stable       | source, all
asciinema  | 2.4.0-1       | testing      | source, all
asciinema  | 2.4.0-1       | unstable     | source, all

LinuxではReleasesページにx86_64, aarch64のバイナリが用意されています.

armhfでのビルド

Raspberry Pi OS bookworm armhf環境でビルドを試してみました.

パッケージで導入したRustが古いようです,rust 1.66以降が必要そうです.

$ git clone https://github.com/asciinema/asciinema
$ cd asciinema
$ cargo build --release
  :
error: package `toml v0.8.23` cannot be built because it requires rustc 1.66 or newer, while the currently active rustc version is 1.63.0
Either upgrade to rustc 1.66 or newer, or use
cargo update -p toml@0.8.23 --precise ver
where `ver` is the latest version of `toml` supporting rustc 1.63.0

以下のManual installationからarmv7-unknown-linux-gnueabihf版のrustup-initを入手します.

$ wget https://static.rust-lang.org/rustup/dist/armv7-unknown-linux-gnueabihf/rustup-init
    https://static.rust-lang.org/rustup/dist/armv7-unknown-linux-gnueabihf/rustup-init.sha256
$ sha256sum --check rustup-init.sha256
./rustup-init: OK
$ chmod u+x ./rustup-init

rustup-initコマンドを実行して新しいrustを導入します.

$ ./rustup-init
  :
Continue? (y/N) y
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>1
  :
info: installing component 'rustfmt'
info: default toolchain set to 'stable-armv7-unknown-linux-gnueabihf'

  stable-armv7-unknown-linux-gnueabihf installed - rustc 1.90.0 (1159e78c4 2025-09-14)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
source $"($nu.home-path)/.cargo/env.nu"  # For nushell

バージョンを確認します.最近リリースされた1.90.0が使えるようになりました.

$ which cargo
/usr/bin/cargo
$ /usr/bin/cargo -V
cargo 1.65.0
$ ~/.cargo/bin/cargo -V
warning: `/home/matoken/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
cargo 1.90.0 (840b83a10 2025-07-30)

rust 1.90.0を使って再度asciinemaをbuildします.今度は成功.

$ PATH=~/.cargo/bin:$PATH cargo build --release

    Finished `release` profile [optimized] target(s) in 39m 49s

asciinemaのusageとversionを確認.3.0.0になっています.

$ target/release/asciinema
Terminal session recorder, streamer, and player

Usage: asciinema [OPTIONS] <COMMAND>

Commands:
  record   Record a terminal session [aliases: rec]
  stream   Stream a terminal session
  session  Record and stream a terminal session
  play     Play back a terminal session
  upload   Upload a recording to an asciinema server
  auth     Authenticate this CLI with an asciinema server account
  cat      Concatenate multiple recordings
  convert  Convert a recording to another format
  help     Print this message or the help of the given subcommand(s)

Options:
  -q, --quiet    Quiet mode - suppress diagnostic messages
  -h, --help     Print help (see more with '--help')
  -V, --version  Print version

$ target/release/asciinema --version
asciinema 3.0.0

asciinema 3,0を試す

ライブストリーミング機能を試す

新機能のライブストリーミング機能を試します.
ローカルとリモートが選べます.

$ target/release/asciinema stream
error: the following required arguments were not provided:
  <--local [<IP:PORT>]|--remote [<STREAM-ID|WS-URL>]>

Usage: asciinema stream <--local [<IP:PORT>]|--remote [<STREAM-ID|WS-URL>]>

For more information, try '--help'.
$ target/release/asciinema stream -h
Stream a terminal session

Usage: asciinema stream [OPTIONS] <--local [<IP:PORT>]|--remote [<STREAM-ID|WS-URL>]>

Options:
  -l, --local [<IP:PORT>]            Stream via the local HTTP server
  -r, --remote [<STREAM-ID|WS-URL>]  Stream via remote asciinema server
  -c, --command <COMMAND>            Command to start in the session [default: $SHELL]
  -I, --capture-input                Enable input (keyboard) capture
      --capture-env <VARS>           Comma-separated list of environment variables to capture
                                     [default: SHELL]
  -t, --title <TITLE>                Title of the session
      --headless                     Headless mode - don't use the terminal for I/O
      --window-size <COLSxROWS>      Override session's terminal window size
      --return                       Return the session's exit status
      --log-file <PATH>              Log file path
      --server-url <URL>             asciinema server URL
  -q, --quiet                        Quiet mode - suppress diagnostic messages
  -h, --help                         Print help (see more with '--help')

Examples:

  asciinema stream --local
      Streams a shell session via the local HTTP server listening on an ephemeral port on 127.0.0.1

  asciinema stream --local 0.0.0.0:8080
      Streams via the local HTTP server listening on port 8080 on all network interfaces

  asciinema stream --remote
      Streams via an asciinema server for public viewing

  asciinema stream -l -r
      Streams both locally and remotely simultaneously

  asciinema stream -r --command "ping asciinema.org"
      Streams execution of the ping command

  asciinema stream -r <ID> -t "Live coding"
      Streams via a remote server, reusing the existing stream ID and setting the stream title

まずはローカルを試します.既定値ではlocalhostしか使えないようなのでlan内で使えるようにipアドレスと適当なportを指定して起動します.

$ target/release/asciinema stream --local 10.42.0.253:9999
::: asciinema session started
::: Live streaming at http://10.42.0.253:9999
::: Press <ctrl+d> or type 'exit' to end
$ ^d
exit
::: asciinema session ended

この状態でウェブブラウザで Live streaming at の後ろのアドレスにアクセスするとasciinema端末のストリーミングが表示されます.

asciinema stream local

次はリモート.
初回は認証が必要です.authサブコマンドを実行したときに表示されるurlにアクセスして認証します.

$ target/release/asciinema auth
Open the following URL in a web browser to authenticate this CLI with your asciinema.org user account:

https://asciinema.org/connect/abc123de-f345-7890-1234-abc123def456

This will associate all recordings uploaded from this machine with your account (including past uploads), and enable public live streaming via asciinema.org.

リモートサーバは未指定時の既定値ではasciinema.orgが利用されます.

$ target/release/asciinema stream --remote
::: asciinema session started
::: Live streaming at https://asciinema.org/s/aBcDeFgHiJkLmNoP
::: Press <ctrl+d> or type 'exit' to end

この状態でウェブブラウザで Live streaming at の後ろのアドレスにアクセスするとasciinema.orgで端末のストリーミングが表示されます.

閲覧のみですがデモにいい感じそうです.

録画と再生

以前は録画時にリモートにアップロードすると録画ファイルは消えていましたが,asciinema 3.0ではローカルにファイルが残るようになっています.
また,録画形式もasciicast v2, asciicast v3(default), raw, txtから選べるようになっています.基本的に規定値のasciicast v3形式でいいでしょうが,セルフホスト環境などでv3非対応の場合などはv2形式にする感じでしょうか.もしv3で録画したあとv2が必要だった(もしくはその逆)の場合でも後述の変換コマンドで相互変換が可能です.

$ target/release/asciinema rec -h                                                                                                                [4/1462]
Record a terminal session

Usage: asciinema record [OPTIONS] <FILE>

Arguments:
  <FILE>  Output file path

Options:
  -f, --output-format <FORMAT>   Output file format [default: asciicast-v3] [possible values:
                                 asciicast-v3, asciicast-v2, raw, txt]
  -c, --command <COMMAND>        Command to start in the session [default: $SHELL]
  -I, --capture-input            Enable input (keyboard) capture
      --capture-env <VARS>       Comma-separated list of environment variables to capture [default:
                                 SHELL]
  -a, --append                   Append to an existing recording file
      --overwrite                Overwrite the output file if it already exists
  -t, --title <TITLE>            Title of the recording
  -i, --idle-time-limit <SECS>   Limit idle time to a given number of seconds
      --headless                 Headless mode - don't use the terminal for I/O
      --window-size <COLSxROWS>  Override session's terminal window size
      --return                   Return the session's exit status
      --log-file <PATH>          Log file path
  -q, --quiet                    Quiet mode - suppress diagnostic messages
  -h, --help                     Print help (see more with '--help')

Examples:

  asciinema rec demo.cast
      Records a shell session to a file

  asciinema rec --command "python script.py" demo.cast
      Records execution of a Python script

  asciinema rec --idle-time-limit 2 demo.cast
      Records with idle time capped at 2 seconds

  asciinema rec --capture-input --title "API Demo" demo.cast
      Records with keyboard input and sets a title

  asciinema rec --append demo.cast
      Continues recording to an existing file

  asciinema rec demo.txt
      Records as a plain-text log - output format inferred from the .txt extension

録画してみる.

$ target/release/asciinema rec hello-asciinema3.0.cast
::: asciinema session started
::: Recording to hello-asciinema3.0.cast
::: Press <ctrl+d> or type 'exit' to end
$
$ ^d
exit
::: asciinema session ended
::: Recorded to hello-asciinema3.0.cast

録画ファイルから再生.

$ target/release/asciinema play hello-asciinema3.0.cast
::: Replaying session from hello-asciinema3.0.cast
$
exit
::: Playback ended

録画ファイルのasciinema.orgへのアップロード

asciicast v1, v2, v3形式のファイルをasciinemaサーバにアップロードします.今回は既定値のasciinema.orgへアップロードします.

$ target/release/asciinema upload -h
Upload a recording to an asciinema server

Usage: asciinema upload [OPTIONS] <FILE>

Arguments:
  <FILE>  The path to the asciicast recording file to upload, in a supported asciicast format (v1,
          v2, or v3)

Options:
      --server-url <URL>  asciinema server URL
  -q, --quiet             Quiet mode - suppress diagnostic messages
  -h, --help              Print help (see more with '--help')

録画ファイルを指定してアップロードします.成功したら共有urlが表示されます.

$ target/release/asciinema upload hello-asciinema3.0.cast
View the recording at:

    https://asciinema.org/a/OyMKojer2JRgnQbICxZ14mmKC
Tip

大きなファイルはアップロードに失敗します.asciinema serverをセルフホストしたり,asciinema playerをセルフホストすることで大きなファイルを共有することが可能です.

asciicast形式の変換

asciinema 3.0でファイル形式がasciicast v3になりました.
convertコマンドでasciicast v1, v2, v3を相互に変換可能になっています.また,rawからのasciicast形式への変換もできるようです.以前は別のサードパーティツールを使っていましたがasciinemaコマンドだけで完結できるようになったようです.

$ target/release/asciinema convert -h
Convert a recording to another format

Usage: asciinema convert [OPTIONS] <INPUT> <OUTPUT>

Arguments:
  <INPUT>   The source recording to convert. Can be a local file path, HTTP(S) URL for remote files,
            or '-' to read from standard input. Remote URLs allow converting recordings directly
            from the web without need for manual downloading. Supported input formats include
            asciicast v1, v2 and v3
  <OUTPUT>  The output path for the converted recording. Can be a file path or '-' to write to
            standard output

Options:
  -f, --output-format <FORMAT>  Output file format [default: asciicast-v3] [possible values:
                                asciicast-v3, asciicast-v2, raw, txt]
      --overwrite               Overwrite the output file if it already exists
  -q, --quiet                   Quiet mode - suppress diagnostic messages
  -h, --help                    Print help (see more with '--help')

Examples:

  asciinema convert old.cast new.cast
      Converts a recording to the latest asciicast format (v3)

  asciinema convert demo.cast demo.txt
      Exports a recording as a plain-text log - output format inferred from the .txt extension

  asciinema convert --output-format raw demo.cast demo.txt
      Exports as raw terminal output

  asciinema convert -f txt demo.cast -
      Exports as plain text to stdout

  asciinema convert https://asciinema.org/a/569727 starwars.cast
      Downloads a remote recording and converts it to the latest asciicast format (v3)

変換してみます.

asciicast v3からv2へ変換
$ target/release/asciinema convert --output-format asciicast-v2 hello-asciinema3.0.cast hello-asciinema3.0.v2.cast
asciicast v2からv3へ変換
$ target/release/asciinema convert --output-format asciicast-v3 hello-asciinema3.0.v2.cast hello-asciinema3.0.v3.cast

変換後のファイルをplayコマンドで再生してみると問題なく再生できました.

おわりに

asciinema 3.0がリリースされました.標準でローカルに録画ファイルが残るようになったり,ストリーミング機能,ファイル形式変換機能が便利そうです.
その他asciicast v3形式に対応したplayerも試してみたいところです.

]]>
https://matoken.org/blog/2025/09/23/building-in-asciinema-3-0-and-raspberry-pi-armhf-environments/feed/ 0
Cargoで導入したRustパッケージをcargo-updateで一括更新 https://matoken.org/blog/2024/10/25/bulk-update-rust-packages-introduced-with-cargo-using-cargo-update/ https://matoken.org/blog/2024/10/25/bulk-update-rust-packages-introduced-with-cargo-using-cargo-update/#comments Thu, 24 Oct 2024 22:04:46 +0000 https://matoken.org/blog/?p=4178

cargo install で導入したものを一括アップデートしたいなと思い cargo-update を試してみました.

install
$ cargo install cargo-update
help
$ cargo install-update --help
cargo-install-update 15.0.0
наб <nabijaczleweli@nabijaczleweli.xyz>
Yann Simon <yann.simon.fr@gmail.com>
ven <vendethiel@hotmail.fr>
Cat Plus Plus <piotrlegnica@piotrl.pl>
Liigo <liigo@qq.com>
azyobuzin <azyobuzin@users.sourceforge.jp>
Tatsuyuki Ishi <ishitatsuyuki@gmail.com>
Tom Prince <tom.prince@twistedmatrix.com>
Mateusz Mikuła <mati865@gmail.com>
sinkuu <sinkuupump@gmail.com>
Alex Burka <aburka@seas.upenn.edu>
Matthias Krüger <matthias.krueger@famsik.de>
Daniel Holbert <dholbert@cs.stanford.edu>
Jonas Bushart <jonas@bushart.org>
Harrison Metzger <harrisonmetz@gmail.com>
Benjamin Bannier <bbannier@gmail.com>
Dimitris Apostolou <dimitris.apostolou@icloud.com>
Corbin Uselton <corbinu@decimal.io>
QuarticCat <QuarticCat@protonmail.com>
Artur Sinila <freesoftware@logarithmus.dev>
qthree <qthree3@gmail.com>
Marcin Puc <tranzystorek.io@protonmail.com>
Paul Barker <paul@pbarker.dev>
Benoît CORTIER <bcortier@proton.me>
Biswapriyo Nath <nathbappai@gmail.com>
Shiraz <smcclennon@protonmail.com>
Victor Song <vms2@rice.edu>
chrisalcantara <chris@chrisalcantara.com>
Utkarsh Gupta <utkarshgupta137@gmail.com>
nevsal
A cargo subcommand for checking and applying updates to installed executables

USAGE:
    cargo install-update [OPTIONS] [--] [PACKAGE]...

ARGS:
    <PACKAGE>...    Packages to update

OPTIONS:
    -a, --all                           Update all packages
    -c, --cargo-dir <CARGO_DIR>         The cargo home directory. Default: $CARGO_HOME or $HOME/.cargo [aliases: root]
    -d, --downdate                      Downdate packages to match latest unyanked registry version
    -f, --force                         Update all packages regardless if they need updating
    -g, --git                           Also update git packages
    -h, --help                          Print help information
    -i, --allow-no-update               Allow for fresh-installing packages
    -j, --jobs <JOBS>                   Limit number of parallel jobs.
    -l, --list                          Don't update packages, only list and check if they need an update (all packages by default)
        --locked                        Enforce packages' embedded Cargo.lock
    -q, --quiet                         No output printed to stdout
    -r, --install-cargo <EXECUTABLE>    Specify an alternative cargo to run for installations
    -s, --filter <PACKAGE_FILTER>       Specify a filter a package must match to be considered
    -t, --temp-dir <TEMP_DIR>           The temporary directory. Default: $TEMP/cargo-update
    -V, --version                       Print version information

-l / --list でアップデートの確認ができます.

$ cargo install-update --list
    Polling registry 'https://index.crates.io/'.........

Package       Installed  Latest   Needs update
impala        v0.2.1     v0.2.3   Yes
pik           v0.7.0     v0.9.0   Yes
sniffnet      v1.0.1     v1.3.1   Yes
tuisky        v0.0.5     v0.1.2   Yes
zeitfetch     v0.1.12    v0.1.13  Yes
cargo-update  v15.0.0    v15.0.0  No
monolith      v2.8.3     v2.8.3   No
tui-journal   v0.12.0    v0.12.0  No
zellij        v0.40.1    v0.40.1  No

-a / --all でアップデートできるようです.
index アップデート,ダウンロード,コンパイルが実行されます.

$ cargo install-update --all
    Polling registry 'https://index.crates.io/'.........
    :

再度 -l / --list で確認すると Installed と Latest が同じになり,Needs update が全て No になっています.最新になったようです.

$ cargo install-update --list
    Polling registry 'https://index.crates.io/'.........

Package       Installed  Latest   Needs update
cargo-update  v15.0.0    v15.0.0  No
impala        v0.2.3     v0.2.3   No
monolith      v2.8.3     v2.8.3   No
pik           v0.9.0     v0.9.0   No
sniffnet      v1.3.1     v1.3.1   No
tui-journal   v0.12.0    v0.12.0  No
tuisky        v0.1.2     v0.1.2   No
zeitfetch     v0.1.13    v0.1.13  No
zellij        v0.40.1    v0.40.1  No

大分楽になりました.

環境
$ dpkg-query -W cargo
cargo   1.81.0+dfsg1-2
$ lsb_release -dr
Description:    Debian GNU/Linux trixie/sid
Release:        n/a
$ arch
x86_64
]]>
https://matoken.org/blog/2024/10/25/bulk-update-rust-packages-introduced-with-cargo-using-cargo-update/feed/ 1
CLI なワンタイムパスワードマネージャの cloak を試す https://matoken.org/blog/2018/09/23/try-cloak-of-cli-one-time-password-manager/ https://matoken.org/blog/2018/09/23/try-cloak-of-cli-one-time-password-manager/#respond Sun, 23 Sep 2018 12:47:12 +0000 http://matoken.org/blog/?p=2130

Rust製のコマンドラインで動作するワンタイムパスワードマネージャです.TOTP/HOTP に対応しています.

Table of Contents

導入

Rust package managerのcargoを導入
$ sudo apt install cargo
cargoでcloakを導入
$ cargo install cloak

~/.cargo/ 以下に導入される

試してみる

help
$ ~/.cargo/bin/cloak --help
cloak 0.1.0
Evans Murithi <murithievans80@gmail.com>
A Command Line OTP Authenticator application.

USAGE:
	cloak [SUBCOMMAND]

FLAGS:
	-h, --help       Prints help information
	-V, --version    Prints version information

SUBCOMMANDS:
	add               Add a new account
	delete            Delete an account
	help              Prints this message or the help of the given subcommand(s)
	list              List OTP for all accounts
	recovery_codes    View recovery codes for an account
	view              View the OTP for an account
add サブコマンドで登録する
$ ~/.cargo/bin/cloak add --help
cloak-add
Add a new account

USAGE:
	cloak add [FLAGS] [OPTIONS] <account> <key>

FLAGS:
	-h, --help       Prints help information
		--hotp       Counter based account
		--totp       Time based account (default)
	-V, --version    Prints version information

OPTIONS:
	-a, --algorithm <ALGORITHM>    Algorithm to use to generate the OTP code [possible values: SHA1, SHA256, SHA384,
								   SHA512, SHA512_256]

ARGS:
	<account>    Name of the account
	<key>        Secret key of the OTP
$ ~/.cargo/bin/cloak add matoken@home.nextcloud M0KMYFVOZY7AGX47
Account successfully created
登録情報確認
$ ~/.cargo/bin/cloak list
Account: matoken@home.nextcloud
TOTP: 538273
アカウントを指定してTOTPトークン表示
$ ~/.cargo/bin/cloak view matoken@home.nextcloud
370696
設定ファイルには生で情報が格納されるので取扱注意
$ cat ~/.cloak/accounts
["matoken@home.nextcloud"]
key = "M0KMYFVOZY7AGX47"
totp = true
hash_function = "SHA1"
アクセス権もそのままでは危ないので注意
$ ls -l ~/.cloak/accounts
-rw-r--r-- 1 matoken matoken 87  9月 22 06:19 /home/matoken/.cloak/accounts
$ chmod -R go-rx ~/.cloak
$ chattr +s ~/.cloak/accounts
$ ls -la ~/.cloak
合計 12
drwx------  2 matoken matoken 4096  9月 22 06:19 .
drwxr-xr-x 62 matoken matoken 4096  9月 23 19:58 ..
-rw-------  1 matoken matoken   87  9月 22 06:19 accounts

秘密鍵が平文でそのままファイルに格納されるので最低でもecryptfsは設定しておかないとかなな感じ?

私は現在はoathtoolを使ったscriptを使っています.秘密鍵はファイルに手で書いて暗号化しておきscript実行時に復号してoathtoolに渡してTOTP表示という感じ.若しくはKeePassCX.

環境

$ ~/.cargo/bin/cloak -V
cloak 0.1.0
$ dpkg-query -W cargo
cargo   0.29.0-1
$ lsb_release -d
Description:    Debian GNU/Linux unstable (sid)
$ uname -m
x86_64
]]>
https://matoken.org/blog/2018/09/23/try-cloak-of-cli-one-time-password-manager/feed/ 0